<?php
require_once('../../core.inc.php');
require_once('Model/SiteManage.php');
require_once('View/Smarty.php');
require_once('Model/DB.php');
require_once('Model/Pager.php');
require_once('Model/Uploader.php');
require_once('Plugin/Sample.php');

$sys = new Model_SiteManage();
$sys->checkLogin();
$params = $_REQUEST;
$smarty = new View_Smarty($params);
$sample = new Model_Sample();
$smarty->assign('sample', $sample);

if (isset($params['do']) && function_exists('exec_' . $params['do'])){
   $params['op'] = call_user_func('exec_' . $params['do'], &$params, &$smarty, &$sample);
}
if (isset($params['op']) && function_exists('op_' . $params['op'])){
    call_user_func('op_' . $params['op'], $params, &$smarty, &$sample);
}
else{
    call_user_func('op_search', $params, &$smarty, &$sample);
}

// 一覧、検索
function op_search($params, &$smarty, &$sample)
{
    if (!isset($params['search'])){
        $search = array();
    }
    $smarty->assign('search', $params['search']);
    $smarty->display('sample/sample_index.html');
}
// 新規登録
function op_regist($params, &$smarty, &$sample)
{
    if (!isset($params['forms'])){
        $forms = array();
        $params['forms'] = $forms;
    }
    $smarty->assign('phase', 'input');
    $smarty->assign('forms', $params['forms']);
    $smarty->display('sample/sample_form.html');
}
function op_regist_complete($params, &$smarty, &$sample)
{
    $smarty->assign('phase', 'complete');
    if(isset($params['id'])){
        $smarty->assign('id', $params['id']);
        $smarty->assign('forms', array('id' => $params['id']));
    }
    $smarty->assign('message', 'ここに完了メッセージが入ります');
    $smarty->display('sample/sample_form.html');
}
// 編集
function op_edit($params, &$smarty, &$sample)
{
    $id = $params['id'];
    if (!isset($params['forms'])){
        $forms = $sample->get($id);
        $params['forms']   = $forms;
    }
    else{
        /*必要？
        $keys = explode(',', 'status,class_id,park_flag');
        foreach($keys as $key){
            if (!isset($params['forms'][$key])){
                $params['forms'][$key] = '';
            }
        }
         */
    }
    $smarty->assign('id', $id);
    $smarty->assign('phase', 'input');
    $smarty->assign('forms', $params['forms']);
    $smarty->display('sample/sample_form.html');
}
function op_edit_complete($params, &$smarty, &$sample)
{
    $smarty->assign('phase', 'complete');
    if(isset($params['id'])){
        $smarty->assign('id', $params['id']);
        $smarty->assign('forms', array('id' => $params['id']));
    }
    $smarty->assign('message', '更新が完了しました');
    $smarty->display('sample/sample_form.html');
}
// 参照
function op_refer($params, &$smarty, &$sample)
{
    $smarty->display('sample/sample_refer.html');
}

?>